package com.jdew.thkj.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.jdew.thkj.R;
import com.jdew.thkj.application.MyApplaction;
import com.jdew.thkj.bean.VersionBean;
import com.jdew.thkj.dialog.CommonDialog;
import com.jdew.thkj.manager.ActivityManagerUtils;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.APKVersionCodeUtils;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;
import com.king.app.updater.AppUpdater;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 *
 * @author yangzai
 * @date 2018/3/27 0027
 */

public class AboutActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.version)
    TextView version;
    @Bind(R.id.versionView)
    LinearLayout versionView;
    @Bind(R.id.check)
    TextView check;
    @Bind(R.id.checkView)
    LinearLayout checkView;
    @Bind(R.id.title)
    TextView title;
    private Boolean isCheckBoolean = false;
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        StatusBarUtil.setTransparentForImageView(this,null);
        ActivityManagerUtils.pushActivity(this);
        ButterKnife.bind(this);
        initViews();
    }


    public void initViews() {
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        version.setText("V" + APKVersionCodeUtils.getVerName(this));
        checkView.setOnClickListener(this);
        back.setVisibility(View.VISIBLE);
        title.setText("关于淘乐惠1");
        back.setOnClickListener(this);
        GetBanben();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        ActivityManagerUtils.popActivity(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.checkView: {
                isCheckBoolean = true;
                GetBanben();
            }
            break;
            default:
                break;
        }
    }

    public void GetBanben() {
        String timesamp=RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetBanben(RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken",""))).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<VersionBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(VersionBean bean) {
                if (bean.getCode().equals("0")) {
                    String VersionCode = bean.getData().getBnumber();
                    String tit = bean.getData().getContentOne();
                    if (!VersionCode.equals(APKVersionCodeUtils.getVersionCode(AboutActivity.this) + "")) {
                        check.setText("发现新版本");
                        if (isCheckBoolean) {
                            checkVersion(tit);
                        }
                        version.setText("V" + APKVersionCodeUtils.getVerName(AboutActivity.this));
                    } else {
                        if (isCheckBoolean) {
                            ToastUtils.getInstance(AboutActivity.this).showMessage("当前已是最新版本");
                        }
                        check.setText("暂无更新");
                    }
                }
            }
        });
    }

    private void checkVersion(String title) {
        SimpleDateFormat sdf = new SimpleDateFormat("yy.MM.dd");
        Date d = new Date();
        String dateNowStr = sdf.format(d);

        //这里不发送检测新版本网络请求，直接进入下载新版本安装
        final CommonDialog.Builder builder = new CommonDialog.Builder(this, true);
        builder.setTitle(dateNowStr + " 系统更新公告");
        builder.setMessage(title);
        builder.setCancelable(false);
        builder.setPositiveButton("立即升级", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
                new AppUpdater.Builder()
                        .serUrl("http://taolehui.oss-cn-hangzhou.aliyuncs.com/taolehui.apk")
                        .setFilename("taolehui.apk")
                        .build(AboutActivity.this)
                        .start();
            }
        });
        builder.setNegativeButton(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                builder.dismiss();
            }
        });
        builder.create().show();
    }
}
